package top.bbman.boot.repository.vo;

import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.Data;
import top.bbman.boot.repository.base.PeaErrorCode;
import top.bbman.boot.repository.enums.PeaCodeEnum;
import top.bbman.boot.repository.function.ConvertFunction;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 * 分页返回数据
 *
 * @author 祥霸
 * @version 1.0
 * @param <T>
 */
@Data
public class PeaPageOut<T> implements Serializable {

    private int code;

    private String desc;

    private PeaPageData<T> data;


    public static PeaPageOut<String> success() {
        return PeaPageOut.buildResponse(PeaCodeEnum.SUCCESS.code(), "", null);
    }

    public static <T> PeaPageOut<T> success(IPage<T> data) {
        return PeaPageOut.buildResponse(PeaCodeEnum.SUCCESS.code(), "", data);
    }


    public static <T, S> PeaPageOut<T> success(IPage<S> data, ConvertFunction<T, S> convertFunction) {
        PeaPageOut<T> out = new PeaPageOut<>(PeaCodeEnum.SUCCESS.code(), "");
        PeaPageData<T> pageData = new PeaPageData<>();
        pageData.setTotal(data.getTotal());
        List<T> dtoList = new ArrayList<>();
        for (S s : data.getRecords()) {
            dtoList.add(convertFunction.sToT(s));
        }
        pageData.setList(dtoList);
        out.setData(pageData);
        return out;
    }

    public static <T> PeaPageOut<T> success(List<T> list) {
        PeaPageOut<T> out = new PeaPageOut<>(PeaCodeEnum.SUCCESS.code(), "");
        PeaPageData<T> pageData = new PeaPageData<>();
        if(Objects.isNull(list)) {
            list = new ArrayList<>();
        }
        pageData.setTotal(list.size());
        pageData.setList(list);
        out.setData(pageData);
        return out;
    }

    public static <T> PeaPageOut<T> success(Long total, List<T> list) {
        PeaPageOut<T> out = new PeaPageOut<>(PeaCodeEnum.SUCCESS.code(), "");
        PeaPageData<T> pageData = new PeaPageData<>();
        pageData.setTotal(total);
        if(Objects.isNull(list)) {
            list = new ArrayList<>();
        }
        pageData.setList(list);
        out.setData(pageData);
        return out;
    }

    public static <T> PeaPageOut<T> build(PeaErrorCode errorCode) {
        return new PeaPageOut<>(errorCode.code(), errorCode.msg());
    }

    public static <T> PeaOut<T> build(int code, String msg) {
        return new PeaOut<>(code, msg);
    }

    public static <T> PeaOut<T> build(PeaErrorCode errorCode, T data) {
        return new PeaOut<>(errorCode.code(), errorCode.msg(), data);
    }

    public int getCode() {
        return code;
    }

    public void setCode(int code) {
        this.code = code;
    }

    public String getDesc() {
        return desc;
    }

    public void setDesc(String desc) {
        this.desc = desc;
    }

    public boolean isSuccess() {
        return PeaCodeEnum.SUCCESS.code() == this.code;
    }

    public static <T> PeaPageOut<T> buildResponse(int code, String msg, IPage<T> data) {
        return new PeaPageOut<>(code, msg, data);
    }

    public PeaPageOut(int code, String desc, IPage<T> page) {

        PeaPageData<T> pageData = new PeaPageData<>();

        if(Objects.nonNull(page)) {
            pageData.setTotal(page.getTotal());
            pageData.setList(page.getRecords());
        }


        this.code = code;
        this.desc = desc;
        this.data = pageData;
    }

    public PeaPageOut(int code, String desc) {
        this.code = code;
        this.desc = desc;
    }
}
